﻿using ezCloud.Api.Common;
using ezCloud.Api.Models;
using ezCloud.Data;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web;
using System.Web.Http;

namespace ezCloud.Api.Controllers
{
    [RoutePrefix("api/ExtraService")]
    public class ExtraServiceController : ezController
    {
        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("AllExtraServiceTypes")]
        public List<ExtraServiceTypes> getAllExtraServiceTypes()
        {
            var user = Common.UserUtitily.getUserProfile();
            
            return context.ExtraServiceTypes.Where(esTypes => esTypes.HotelId == user.HotelId).ToList();
        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("ExtraServiceCategoriesByType")]
        public List<ExtraServiceCategories> getExtraServiceCategoriesByType(int extraServiceTypeId)
        {
            var currentClientUser = Common.UserUtitily.getUserProfile();
            

           ExtraServiceTypes esType = context.ExtraServiceTypes.Where(type => type.HotelId == currentClientUser.HotelId && type.ExtraServiceTypeId == extraServiceTypeId).FirstOrDefault();
           if (esType == null)
           {
               throw new HttpResponseException(HttpStatusCode.NotFound);
               //return NotFound();
           }

           return context.ExtraServiceCategories.Where(esCategories => esCategories.ExtraServiceTypeId == esType.ExtraServiceTypeId).ToList();
        }

       



        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("ExtraServiceItemsByCategory")]
        public List<ExtraServiceItems> getExtraServiceItemsByCategory(int extraServiceCategoryId)
        {
            var currentClientUser = Common.UserUtitily.getUserProfile();
            

            ExtraServiceCategories esCategory = context.ExtraServiceCategories.Where(cat => cat.HotelId == currentClientUser.HotelId && cat.ExtraServiceCategoryId == extraServiceCategoryId).FirstOrDefault();
            if (esCategory == null)
            {
                throw new HttpResponseException(HttpStatusCode.NotFound);
            }

            return context.ExtraServiceItems.Where(esItems => esItems.ExtraServiceCategoryId == esCategory.ExtraServiceCategoryId).ToList();
        }

       /* [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("SaveExtraServiceItemList")]
        public async Task<IHttpActionResult> SaveExtraServiceItemList(SaveExtraServiceItemsByCategory model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            
            var currentClientUser = Common.UserUtitily.getUserProfile();
            var extraServiceCat = context.ExtraServiceCategories.Where(cat => cat.HotelId == currentClientUser.HotelId && cat.ExtraServiceCategoryId == model.CategoryId).FirstOrDefault();
            if (extraServiceCat == null) return BadRequest("ERROR_NOT_VALID_DATA " + currentClientUser.HotelId);

            List<ExtraServiceItems> listOldItem = context.ExtraServiceItems.Where(it => it.HotelId == currentClientUser.HotelId && it.ExtraServiceCategoryId == model.CategoryId).ToList();

            for (int i = 0; i < model.ItemList.Length; i++)
            {
                var modelItem = model.ItemList[i];
                if (model.ItemList[i].ExtraServiceItemId > 0)
                {
                    //var oldItem = context.ExtraServiceItems.Where(it => it.HotelId == currentClientUser.HotelId && it.ExtraServiceCategoryId == model.CategoryId && it.ExtraServiceItemId == model.ItemList[i].ExtraServiceItemId).FirstOrDefault();
                    var oldItem = listOldItem.FirstOrDefault(old => old.ExtraServiceItemId == model.ItemList[i].ExtraServiceItemId);
                    if (oldItem != null)
                    {
                        oldItem.Unit = modelItem.Unit;
                        oldItem.Price = modelItem.Price;
                        oldItem.ExtraServiceItemName = modelItem.ExtraServiceItemName;
                        listOldItem.Remove(oldItem);
                    }
                }
                else {
                    ExtraServiceItems newESItem = new Data.ExtraServiceItems();
                    newESItem.ExtraServiceItemName = model.ItemList[i].ExtraServiceItemName;
                    newESItem.Unit = model.ItemList[i].Unit;
                    newESItem.Price = model.ItemList[i].Price;
                    newESItem.HotelId = currentClientUser.HotelId;
                    newESItem.ExtraServiceTypeId = extraServiceCat.ExtraServiceTypeId;
                    newESItem.ExtraServiceCategoryId = model.CategoryId;
                    context.ExtraServiceItems.InsertOnSubmit(newESItem);
                }
                
            }
            context.ExtraServiceItems.DeleteAllOnSubmit(listOldItem);
            context.SubmitChanges();

            return Ok();
        }*/

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("AddCategory")]
        public async Task<IHttpActionResult> AddCategory(ExtraServiceCategoryViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            
            var currentClientUser = Common.UserUtitily.getUserProfile();

            var extraServiceType = context.ExtraServiceTypes.Where(typ => typ.HotelId == currentClientUser.HotelId && typ.ExtraServiceTypeId == model.ExtraServiceTypeId).FirstOrDefault();
            if (extraServiceType == null)
            {
                return BadRequest("EXTRA_SERVICE_TYPE_NOT_FOUND");
            }
            ExtraServiceCategories newCategory = new Data.ExtraServiceCategories();
            newCategory.ExtraServiceCategoryName = model.ExtraServiceCategoryName;
            newCategory.ExtraServiceCategoryDescription = model.ExtraServiceCategoryDescription;
            newCategory.ExtraServiceTypeId = extraServiceType.ExtraServiceTypeId;
            newCategory.HotelId = currentClientUser.HotelId;
            newCategory.IsHidden = model.IsHidden;
            context.ExtraServiceCategories.InsertOnSubmit(newCategory);
            context.SubmitChanges();
            Common.Elastic.IndexLastAudit(context);
            return Ok(newCategory);
        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("EditCategory")]
        public async Task<IHttpActionResult> EditCategory(ExtraServiceCategoryViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            
            var currentClientUser = Common.UserUtitily.getUserProfile();

            var extraServiceType = context.ExtraServiceTypes.Where(typ => typ.HotelId == currentClientUser.HotelId && typ.ExtraServiceTypeId == model.ExtraServiceTypeId).FirstOrDefault();
            if (extraServiceType == null)
            {
                return BadRequest("EXTRA_SERVICE_TYPE_NOT_FOUND");
            }

            ExtraServiceCategories oldCategory = context.ExtraServiceCategories.Where(cat => cat.ExtraServiceTypeId == extraServiceType.ExtraServiceTypeId && cat.ExtraServiceCategoryId == model.ExtraServiceCategoryId).FirstOrDefault();
            if (oldCategory == null)
            {
                return BadRequest("Error");
            }

            ExtraServiceCategories newCategory = new Data.ExtraServiceCategories();
            newCategory.ExtraServiceCategoryName = model.ExtraServiceCategoryName;
            newCategory.ExtraServiceCategoryDescription = model.ExtraServiceCategoryDescription;
            newCategory.ExtraServiceTypeId = extraServiceType.ExtraServiceTypeId;
            newCategory.HotelId = currentClientUser.HotelId;
            newCategory.ExtraServiceCategoryId = model.ExtraServiceCategoryId;

            var objDiffer = Utilities.GetDifferentContent(newCategory,oldCategory);

            //ExtraServiceCategories newCategory = new Data.ExtraServiceCategories();
            oldCategory.ExtraServiceCategoryName = model.ExtraServiceCategoryName;
            oldCategory.ExtraServiceCategoryDescription = model.ExtraServiceCategoryDescription;
            oldCategory.ExtraServiceTypeId = extraServiceType.ExtraServiceTypeId;
            oldCategory.HotelId = currentClientUser.HotelId;
            //context.ExtraServiceCategories.InsertOnSubmit(oldCategory);
            context.SubmitChanges();
            Common.Elastic.IndexLastAudit(context);
            Utilities.AddLog("CATEGORY_UPDATE", oldCategory.ExtraServiceCategoryId.ToString(), objDiffer);

            return Ok();
        }


        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("RemoveCategory")]
        public async Task<IHttpActionResult> RemoveCategory(ExtraServiceCategoryViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            
            var currentClientUser = Common.UserUtitily.getUserProfile();

            var extraServiceType = context.ExtraServiceTypes.Where(typ => typ.HotelId == currentClientUser.HotelId && typ.ExtraServiceTypeId == model.ExtraServiceTypeId).FirstOrDefault();
            if (extraServiceType == null)
            {
                return BadRequest("EXTRA_SERVICE_TYPE_NOT_FOUND");
            }

            //ExtraServiceCategories categoryDeleted = context.ExtraServiceCategories.Where(cat => cat.HotelId == currentClientUser.HotelId && cat.ExtraServiceCategoryId == model.ExtraServiceCategoryId).FirstOrDefault();
            ExtraServiceCategories categoryDeleted = context.ExtraServiceCategories.Where(cat => cat.ExtraServiceTypeId == extraServiceType.ExtraServiceTypeId && cat.ExtraServiceCategoryId == model.ExtraServiceCategoryId).FirstOrDefault();
            if (categoryDeleted == null)
            {
                return BadRequest("CATEGORY_NOT_FOUND");
            }

            var listItemInCategoryDeleted = context.ExtraServiceItems.Where(itm => itm.ExtraServiceCategoryId == model.ExtraServiceCategoryId).ToList();
            if (listItemInCategoryDeleted != null)
            {
                context.ExtraServiceItems.DeleteAllOnSubmit(listItemInCategoryDeleted);
            }

            context.ExtraServiceCategories.DeleteOnSubmit(categoryDeleted);
            context.SubmitChanges();
            Common.Elastic.IndexLastAudit(context);
            return Ok();
        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("ChangeCategoryStatus")]
        public async Task<IHttpActionResult> ChangeCategoryStatus(int categoryId)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            
            var currentClientUser = Common.UserUtitily.getUserProfile();

            ExtraServiceCategories esCat = context.ExtraServiceCategories.Where(cat => cat.ExtraServiceCategoryId == categoryId).FirstOrDefault();
            List<ExtraServiceItems> esItemsByCat = context.ExtraServiceItems.Where(itm => itm.ExtraServiceCategoryId == categoryId).ToList();


            if (esCat.IsHidden == false)
            {
                esCat.IsHidden = true;
                
            }
            else
            {
                esCat.IsHidden = false;
            }

            for (int i = 0; i < esItemsByCat.Count; i++)
            {
                esItemsByCat[i].IsHidden = esCat.IsHidden;
            }
            context.SubmitChanges();
            Common.Elastic.IndexLastAudit(context);
            return Ok();

        }


        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("AddItem")]
        public async Task<IHttpActionResult> AddItem(ExtraServiceItemsViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            
            var currentClientUser = Common.UserUtitily.getUserProfile();

            var extraServiceType = context.ExtraServiceTypes.Where(typ => typ.HotelId == currentClientUser.HotelId && typ.ExtraServiceTypeId == model.ExtraServiceTypeId).FirstOrDefault();
            if (extraServiceType == null)
            {
                return BadRequest("Error");
            }
            
            var extraServiceCategory = context.ExtraServiceCategories.Where(cat => cat.ExtraServiceTypeId == model.ExtraServiceTypeId && cat.ExtraServiceCategoryId == model.ExtraServiceCategoryId).FirstOrDefault();
            if (extraServiceCategory == null)
            {
                return BadRequest("Error");
            }


            ExtraServiceItems newItem = new Data.ExtraServiceItems();
            newItem.ExtraServiceItemName = model.ExtraServiceItemName;
            newItem.ExtraServiceItemDescription = model.ExtraServiceItemDescription;
            newItem.ExtraServiceTypeId = extraServiceType.ExtraServiceTypeId;
            newItem.ExtraServiceCategoryId = extraServiceCategory.ExtraServiceCategoryId;
            newItem.HotelId = currentClientUser.HotelId;
            newItem.IsHidden = model.IsHidden;
            newItem.Unit = model.Unit;
            newItem.Price = model.Price;
            newItem.IsChangeable = model.IsChangeable;
            context.ExtraServiceItems.InsertOnSubmit(newItem);
            context.SubmitChanges();
            Common.Elastic.IndexLastAudit(context);
            return Ok(newItem);
        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("EditItem")]
        public async Task<IHttpActionResult> EditItem(ExtraServiceItemsViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            
            var currentClientUser = Common.UserUtitily.getUserProfile();

            var extraServiceType = context.ExtraServiceTypes.Where(typ => typ.HotelId == currentClientUser.HotelId && typ.ExtraServiceTypeId == model.ExtraServiceTypeId).FirstOrDefault();
            if (extraServiceType == null)
            {
                return BadRequest("Error");
            }

            var extraServiceCategory = context.ExtraServiceCategories.Where(cat => cat.ExtraServiceTypeId == model.ExtraServiceTypeId && cat.ExtraServiceCategoryId == model.ExtraServiceCategoryId).FirstOrDefault();
            if (extraServiceCategory == null)
            {
                return BadRequest("Error");
            }

            ExtraServiceItems oldItem = context.ExtraServiceItems.Where(itm => itm.ExtraServiceTypeId == extraServiceType.ExtraServiceTypeId && itm.ExtraServiceCategoryId == model.ExtraServiceCategoryId && itm.ExtraServiceItemId == model.ExtraServiceItemId).FirstOrDefault();
            if (oldItem == null)
            {
                return BadRequest("Error");
            }

           
            oldItem.ExtraServiceItemName = model.ExtraServiceItemName;
            oldItem.ExtraServiceItemDescription = model.ExtraServiceItemDescription;
            oldItem.Unit = model.Unit;
            oldItem.Price = model.Price;
            oldItem.ExtraServiceTypeId = extraServiceType.ExtraServiceTypeId;
            oldItem.HotelId = currentClientUser.HotelId;
            oldItem.ExtraServiceCategoryId = extraServiceCategory.ExtraServiceCategoryId;
            oldItem.IsChangeable = model.IsChangeable;
            context.SubmitChanges();
            Common.Elastic.IndexLastAudit(context);
            return Ok();
        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("RemoveItem")]
        public async Task<IHttpActionResult> RemoveItem(ExtraServiceItemsViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            
            var currentClientUser = Common.UserUtitily.getUserProfile();

            ExtraServiceItems itemDeleted = context.ExtraServiceItems.Where(itm => itm.HotelId == currentClientUser.HotelId && itm.ExtraServiceCategoryId == model.ExtraServiceCategoryId && itm.ExtraServiceTypeId == model.ExtraServiceTypeId && itm.ExtraServiceItemId == model.ExtraServiceItemId).FirstOrDefault();
            if (itemDeleted == null)
            {
                return BadRequest("Error");
            }

            context.ExtraServiceItems.DeleteOnSubmit(itemDeleted);
            context.SubmitChanges();
            Common.Elastic.IndexLastAudit(context);
            return Ok();
        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("ChangeItemStatus")]
        public async Task<IHttpActionResult> ChangeItemStatus(int itemId)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            
            var currentClientUser = Common.UserUtitily.getUserProfile();

            ExtraServiceItems esItem = context.ExtraServiceItems.Where(itm => itm.ExtraServiceItemId == itemId).FirstOrDefault();
            if (esItem.IsHidden == false)
            {
                esItem.IsHidden = true;
            }
            else
            {
                esItem.IsHidden = false;
            }
            context.SubmitChanges();
            Common.Elastic.IndexLastAudit(context);
            return Ok();

        }

        [HostAuthentication(DefaultAuthenticationTypes.ExternalBearer)]
        [Route("SaveItemList")]
        public async Task<IHttpActionResult> SaveItemList(List<ExtraServiceItems> items)
        {
            var currentClientUser = Common.UserUtitily.getUserProfile();
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
            if (items.Count > 0)
            {
                for (int i = 0; i < items.Count; i++)
                {
                    ExtraServiceItems oldItem = context.ExtraServiceItems.Where(it => it.HotelId == currentClientUser.HotelId && it.ExtraServiceItemId == items[i].ExtraServiceItemId).FirstOrDefault();
                    oldItem.Priority = items[i].Priority;
                }
            }
            context.SubmitChanges();
            Common.Elastic.IndexLastAudit(context);
            return Ok(context.ExtraServiceItems.Where(eit => eit.HotelId == currentClientUser.HotelId && eit.ExtraServiceCategoryId == items[0].ExtraServiceCategoryId).ToList());
        }
        

    }
}
